package com.itheima.day09_IO;

import java.io.FileWriter;
import java.io.IOException;

public class Demo01 {
    public static void main(String[] args) {
//        test1();
        test2();
    }

    public static void test1() {
        //TODO
        // 请使用FileWriter向文件输出：HelloWorld
        // 用 try-catch-finally 方式处理异常。
        FileWriter fw = null;
        try {
            fw = new FileWriter("D:\\img1\\HelloWord.txt");
            fw.write("HelloWorld");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public static void test2() {
        //TODO
        // 请使用FileWriter向文件输出：HelloWorld
        // 用 try-with-resource 方式处理异常。

        try (FileWriter fw = new FileWriter("D:\\img1\\HelloWord.txt")) {
            fw.write("HelloWorld");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}